package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.conn;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;



public class orders_List extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public orders_List() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	//根据条件返回订单信息
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		PrintWriter out = response.getWriter();
		JSONArray jsonarray = new JSONArray();
		JSONObject jsonobj = new JSONObject();


		conn cn = new conn();
	
	    String f=request.getParameter("f");

		ResultSet rs=null;

		if(f.equals("1"))
		{
			String mid = request.getParameter("mid");
			String t = request.getParameter("t");
			String where="";
			if(t.equals("1"))
			{
				where=" and oflag='待支付' ";
			}
			else if(t.equals("2"))
			{
				where=" and oflag='已支付' ";
			}
			else if(t.equals("3"))
			{
				where=" and oflag='已完成' ";
			}
			rs=cn.query("select * from  orders a left join roomtype r on a.rid = r.rid" +
					" where  mid="+mid+where+" order by ono desc ");
		}
		else if(f.equals("2"))
		{
			String id = request.getParameter("id");
			rs=cn.query("select * from  orders a left join roomtype r on a.rid = r.rid" +
					" where ono='"+id+"' ");
		}

		while (true)
		{
			try {
				if (!rs.next()) break;
             jsonobj.put("ono", rs.getString("ono"));
             jsonobj.put("rid", rs.getString("rid"));
             jsonobj.put("sdate", rs.getString("sdate"));
             jsonobj.put("edate", rs.getString("edate"));
             jsonobj.put("omoney", rs.getString("omoney"));
             jsonobj.put("smoney", rs.getString("smoney"));
             jsonobj.put("ymoney", rs.getString("ymoney"));
             jsonobj.put("mid", rs.getString("mid"));
				jsonobj.put("rname", rs.getString("rname"));
				jsonobj.put("rpic", rs.getString("rpic"));
             jsonobj.put("mname", rs.getString("mname"));
             jsonobj.put("tel", rs.getString("tel"));
             jsonobj.put("cardno", rs.getString("cardno"));
             jsonobj.put("otime", rs.getString("otime"));
             jsonobj.put("oflag", rs.getString("oflag"));

				if(f.equals("2"))
				{

					ResultSet rs2 = cn.query("select * from  ordersmore where ono='" + rs.getString("ono")+"'");
					JSONArray jsonArray2 = new JSONArray();
					JSONObject jsonobj2 = new JSONObject();
					while (rs2.next()) {
						jsonobj2.put("rno", rs2.getString("rno"));

						jsonArray2.add(jsonobj2);
					}

					jsonobj.element("more",jsonArray2);
				}
		
				jsonarray.add(jsonobj);

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

		out.println(jsonarray);	
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

